﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;

namespace RubricaSentimentale
{
    public static class Utility
    {
        public static void SaveState(string key,object state)
        {
            IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
            if (iss.Contains(key))
                iss[key] = state;
            else
                iss.Add(key, state);
        }

        public static object LoadState(string key)
        {
            IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
            if (iss.Contains(key))
                return iss[key];
            else
                return null;
        }

        public static void Save(object o,Type t)
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream;
            if (!store.FileExists("Saved.xml"))
                stream = store.CreateFile("Saved.xml");
            else
                stream = store.OpenFile("Saved.xml",System.IO.FileMode.Truncate);
            DataContractSerializer serializer = new DataContractSerializer(t);
            serializer.WriteObject(stream, o);
            stream.Close();
        }

        public static object Load(Type t)
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            if (!store.FileExists("Saved.xml"))
                return new ViewModel();
            else
            {
                DataContractSerializer deserializer = new DataContractSerializer(t);
                IsolatedStorageFileStream stream = store.OpenFile("Saved.xml", System.IO.FileMode.Open);
                object o= deserializer.ReadObject(stream);
                stream.Close();
                return o;
                

            }
        }
    }
}
